﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using RookieLibSharp.Configuration;
using RookieLibSharp.Log;
namespace BirthdayRemainder.WinService
{
    class MailClient
    {
        public string SendMailAddr { get; set; }
        public string SendMainPSD { get; set; }
        public string ReceiveMailAddr { get; set; }
        public string MailBody { get; set; }
        public string MailSubject { get; set; }

        public bool SendMail()
        {

            if (!ValidCheck())
            {
#if DEBUG

#else
                return false;
#endif 
            }

            MailMessage MailMsg = new MailMessage();
            MailMsg.From = new MailAddress(SendMailAddr, "BirthdayRemind");
            MailMsg.To.Add(new MailAddress(ReceiveMailAddr));
            MailMsg.IsBodyHtml = false;
            MailMsg.BodyEncoding = Encoding.UTF8;
            MailMsg.Body = Funcs.IsRegSucess()? MailBody:"功能受限，注册后使用";
            MailMsg.Priority = MailPriority.Normal;
            MailMsg.Subject = MailSubject;
            SmtpClient smtpClient = new SmtpClient(Funcs.GetMailSendServer(SendMailAddr), 25);
            smtpClient.Credentials = new NetworkCredential(SendMailAddr, SendMainPSD);
            smtpClient.EnableSsl = false;

            bool bSucess = false;
            try
            {
#if DEBUG
 
#else
                smtpClient.Send(MailMsg);
#endif
                
                bSucess = true;
            }
            catch(System.Exception ex)
            {
                LogHelper.WriteLog(LogErrorLevel.Error, this.GetType(), ex);
                
            }
            MailMsg = null;
            smtpClient = null;
            return bSucess;
        }
        private bool ValidCheck()
        {

            if (SendMailAddr == null || SendMailAddr.Length == 0)
            {
                return false;
            }
            if (SendMainPSD == null || SendMainPSD.Length == 0)
            {
                return false;
            }
            if (ReceiveMailAddr == null || ReceiveMailAddr.Length == 0)
            {
                return false;
            }
            return true;
        }

    }
}
